<template>
    <div id="Login">
        <div class="login-box">
            <h2>安心保险</h2>
            <form>
                <div class="user-box">
                    <input
                        type="text"
                        name=""
                        required=""
                        v-model="input.username"
                    />
                    <label>用户名</label>
                </div>
                <div class="user-box">
                    <input
                        type="password"
                        name=""
                        required=""
                        v-model="input.password"
                    />
                    <label>密码</label>
                </div>
                <div>
                    <el-radio v-model="radio" label="2">管理员</el-radio>
                    <el-radio v-model="radio" label="3">销售员</el-radio>
                </div>
                <a href="#" @click="login">
                    <span></span>
                    <span></span>
                    <span></span>
                    <span></span>
                    登录
                </a>
            </form>
        </div>
    </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapActions } = createNamespacedHelpers("login");
export default {
    data() {
        return {
            input: {
                username: "",
                password: "",
            },
            radio: "2",
        };
    },
    methods: {
        //路由后置

        ...mapActions(["loginAdmin", "salesLogin"]),
        //管理登录
        async login() {
            if (this.radio == "2") {
                const { data } = await this.loginAdmin(this.input);
                console.log(data);
                if (data.code == 0) {
                    this.$message.error("密码错误,账号不存在");
                } else {
                    this.$message({
                        message: "登录成功",
                        type: "success",
                    });
                    this.$router.push("/system/salesList");
                    localStorage.setItem("token", data.token);
                    localStorage.setItem("code", data.data[0].power);
                    localStorage.setItem("names", data.data[0].username);
                }
                // if (

                //     data.data[0].delete == false

                // ) {
                //     this.$message.error("密码错误,账号不存在");
                // } else {
                //     this.$message({
                //         message: "登录成功",
                //         type: "success",
                //     });
                //     // this.$router.push("/system");
                //     console.log(data);
                //     localStorage.setItem("token", data.token);
                //     localStorage.setItem("code", data.data[0].power);
                //     localStorage.setItem("_id", data.data[0]._id);
                //     localStorage.setItem("names", data.data[0].username);
                // }
            } else if (this.radio == "3") {
                const { data } = await this.salesLogin(this.input);
                if (data.code == 0) {
                    this.$message.error("密码错误");
                } else {
                    this.$message({
                        message: "登录成功",
                        type: "success",
                    });
                    this.$router.push("/system");

                    localStorage.setItem("token", data.token);
                    localStorage.setItem("code", 2);
                    localStorage.setItem("_id", data.data[0]._id);
                    localStorage.setItem("names", data.data[0].name);
                }
            }
        },
    },
};
</script>

<style scopde>
@import url("../assets/css/login.css");
html {
    height: 100%;
}
#Login {
    height: 100vh;
    margin: 0;
    padding: 0;
    font-family: sans-serif;
    background: linear-gradient(#141e30, #243b55);
}
</style>